﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Modules.bandits {
    import Classes.*;
    import Classes.util.*;
    import Classes.effects.*;
    import Classes.LogicComponents.*;
    import Display.*;
    import Classes.virals.*;
    import Modules.bandits.transactions.*;

    public class MunicipalComponentPolice extends MunicipalComponentBase {

        protected static var m_allStationsBeingUpgraded:Boolean = false;

        protected var m_endFunc:Function = null;

        public function MunicipalComponentPolice(_arg1:Municipal){
            super(_arg1);
        }
        override public function handlePlayAction():void{
            if (m_allStationsBeingUpgraded){
                return;
            };
            if (((Global.isVisiting()) || (((isHarvestable()) && (!(Global.guide.isActive())))))){
                this.m_endFunc = null;
                super.handlePlayAction();
                this.m_municipal.removeStagePickEffect();
                if (!Global.isVisiting()){
                    BanditUtil.logGameActionStats("municipal", "collect", m_municipal.getItem().name);
                };
                return;
            };
            if (this.isStationGated()){
                Global.world.citySim.banditManager.showPoliceStationGate(this.m_municipal);
                return;
            };
            if (this.isUpgradePossible()){
                this.dealWithUpgrades();
            } else {
                if (this.isMinLevelReached()){
                    UI.displayCopsDialog();
                };
            };
        }
        protected function dealWithUpgrades():void{
            var newLevel:* = 0;
            var newStations:* = null;
            var station:* = null;
            var delay:* = NaN;
            var testLevel:* = 0;
            var max:* = 0;
            var level:* = 0;
            this.m_endFunc = function ():void{
            };
            if (((BanditManager.canUpgradeAllStations()) && ((Global.world.citySim.banditManager.getStationUnlockLevel() == BanditUtil.getPoliceStationLevel())))){
                m_allStationsBeingUpgraded = true;
                BanditManager.upgradeAllStations();
                newLevel = BanditUtil.getPoliceStationLevel();
                newStations = BanditUtil.getPoliceStations();
                for each (station in newStations) {
                    testLevel = BanditUtil.getPoliceStationInstanceLevel(station);
                    if (testLevel == newLevel){
                        BanditUtil.logGameActionStats("municipal", "upgrade_to", station.getItem().name);
                        break;
                    };
                };
                delay = Global.gameSettings.getNumber("actionBarAny", 2);
                TimerUtil.callLater(function ():void{
                    if (isStationGated()){
                        Global.world.citySim.banditManager.showPoliceStationGate(m_municipal);
                    } else {
                        Global.world.citySim.banditManager.onUpgradeGateSuccess(m_municipal);
                    };
                    m_allStationsBeingUpgraded = false;
                }, ((delay * 1000) + 200));
            } else {
                if (this.isCatchUpUpgradePossible()){
                    BanditManager.upgradeCatchUp(m_municipal);
                    m_municipal.doUpgradeAnimations();
                } else {
                    max = BanditUtil.getPoliceStationLevel();
                    level = BanditUtil.getPoliceStationInstanceLevel(m_municipal);
                    if ((((level > 0)) && ((level < max)))){
                        m_municipal.upgradeBuildingIfPossible();
                    };
                };
            };
            GameTransactionManager.addTransaction(new TProcessPoliceUpgrade());
        }
        override public function getProgressBarEndFunction():Function{
            return (this.m_endFunc);
        }
        override public function getToolTipAction():String{
            var _local1:String = m_municipal.getGameModeToolTipAction();
            if (((!(_local1)) && (!(Global.isVisiting())))){
                if (((isHarvestable()) && (!(Global.guide.isActive())))){
                    _local1 = ZLoc.t("Main", "RipeEntertainment");
                } else {
                    if (this.isUpgradePossible()){
                        _local1 = ZLoc.t("Dialogs", "UpgradeToolTip");
                    } else {
                        if (this.isDonutNeeded()){
                            _local1 = ZLoc.t("Dialogs", "NeedDonutToolTip");
                        };
                    };
                };
            };
            return (_local1);
        }
        override protected function isUpgradePossible():Boolean{
            return (((!(Global.isVisiting())) && (((m_municipal.isUpgradePossible()) || (this.isCatchUpUpgradePossible())))));
        }
        protected function isCatchUpUpgradePossible():Boolean{
            return (((!(Global.isVisiting())) && ((((BanditUtil.getPoliceStationInstanceLevel(m_municipal) < BanditUtil.getPoliceStationLevel())) || ((BanditUtil.getPoliceStationInstanceLevel(m_municipal) < Global.world.citySim.banditManager.getStationUnlockLevel()))))));
        }
        protected function isStationGated():Boolean{
            m_municipal.removeAnimatedEffectByClass(ScaffoldEffect);
            return (false);
        }
        protected function isMinLevelReached():Boolean{
            return ((BanditUtil.getPoliceStationLevel() > 1));
        }
        protected function isDonutNeeded():Boolean{
            return ((((BanditManager.numSleepingCops > 0)) && (Global.world.viralMgr.canPost(ViralType.COPS_N_BANDITS_GET_DONUT))));
        }
        override public function enableUpdateArrow():Boolean{
            return (((((!(Global.isVisiting())) && (((((this.isUpgradePossible()) || (this.isStationGated()))) || (this.isDonutNeeded()))))) || (super.enableUpdateArrow())));
        }
        override public function createStagePickEffect():void{
            var _local1:ScaffoldEffect;
            if (((((!(Global.isVisiting())) && (isHarvestable()))) && (!(Global.guide.isActive())))){
                super.createStagePickEffect();
            } else {
                if (this.isUpgradePossible()){
                    createStagePickEffectHelper(StagePickEffect.PICK_UPGRADE);
                } else {
                    if (((this.isDonutNeeded()) && (!(this.isStationGated())))){
                        createStagePickEffectHelper(StagePickEffect.PICK_DONUT);
                    };
                };
            };
            if (this.isStationGated()){
                if (!isHarvestable()){
                    createStagePickEffectHelper(StagePickEffect.PICK_UPGRADE);
                };
                _local1 = (m_municipal.getAnimatedEffectByClass(ScaffoldEffect) as ScaffoldEffect);
                if (_local1 == null){
                    _local1 = new ScaffoldEffect(m_municipal);
                    m_municipal.addAnimatedEffectFromInstance(_local1);
                } else {
                    _local1.reattach();
                };
            } else {
                m_municipal.removeAnimatedEffectByClass(ScaffoldEffect);
            };
        }
        override public function doUpgradeAnimations():void{
            m_municipal.doUpgradeAnimations(this.upgradeAnimationFinish);
        }
        protected function upgradeAnimationFinish():void{
            if (BanditUtil.getPoliceStationLevel() >= 6){
                BanditManager.spawnStationHelicopter();
            };
        }

    }
}//package Modules.bandits 
